package com.haier.dreambox.demo.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.ToggleButton;
import com.haier.dreambox.demo.R;
import com.haier.dreambox.lib.usdklib.udev.ac.ACCommand;
import com.haier.dreambox.lib.usdklib.udev.ac.MCCommand;
import com.haier.dreambox.lib.usdklib.udev.ac.MCOperation;
import com.haier.dreambox.lib.usdklib.wrapper.uPlusManager;
import com.haier.uhome.usdk.api.uSDKDeviceAttribute;
import org.androidannotations.annotations.*;
import org.androidannotations.annotations.res.ColorRes;

import java.util.HashMap;


/**
 * Created by smalant on 14/12/26.
 */

@EFragment(R.layout.layout_control_magic_cube)
public class ControlMagicCubeFragment extends Fragment implements IControlPageInterface {

    private static final String TAG = "ControlMagicCubeFragment";
    @ViewById(R.id.id_tb_power)
    ToggleButton toggleButton;

    @ViewById(R.id.id_textview_dev_name)
    TextView textView;


    @ViewById(R.id.id_textView_temperature)
    TextView tv_temperature;

    @Bean
    MCOperation mc;

    @Bean
    ACCommand acCommand;


    @Bean
    uPlusManager sdkManger;

    @ColorRes(android.R.color.white)
    int whiteColor;

    @ColorRes(android.R.color.holo_red_dark)
    int redColor;

    @ViewById(R.id.id_mode_air)
    Button button_air;

    @ViewById(R.id.id_mode_auto)
    Button button_auto;

    @ViewById(R.id.id_mode_purify)
    Button button_purify;

    @ViewById(R.id.id_mode_purify_dehumi)
    Button button_purify_dehumi;

    @ViewById(R.id.id_mode_purify_humi)
    Button button_purify_humi;

    @ViewById(R.id.id_mode_dehumi)
    Button button_dehumi;

    @ViewById(R.id.id_mode_humi)
    Button button_humi;


    @ViewById(R.id.id_wind_high)
    Button button_wind_high;

    @ViewById(R.id.id_wind_mid)
    Button button_wind_mid;

    @ViewById(R.id.id_wind_low)
    Button button_wind_low;

    @ViewById(R.id.id_wind_auto)
    Button button_wind_auto;

    @ViewById(R.id.id_wind_supper_high)
    Button button_wind_super_high;

    @ViewById(R.id.id_wind_mute)
    Button button_wind_mute;

    String mac;


    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

    }

    @AfterViews
    public void initViews() {
        textView.setText("空气魔方");

        if (mac != null && sdkManger.getDeviceAttributeValues(mac) != null) {
            refreshControlView(sdkManger.getDeviceAttributeValues(mac));
        }
    }

    @AfterInject
    public void init() {
        mac = getArguments().getString("mac");

    }


    @CheckedChange(R.id.id_tb_power)
    void checkedChangeOnPowerToggleButton(CompoundButton power, boolean isChecked) {
        if (isChecked) {
            mc.closeDevice(mac);
        } else {
            mc.openDevice(mac);
        }
    }


    @Click({R.id.id_wind_auto, R.id.id_wind_high, R.id.id_wind_mid, R.id.id_wind_low, R.id.id_wind_mute, R.id.id_wind_supper_high})
    void setWind(View clickedView) {

        switch (clickedView.getId()) {
            case R.id.id_wind_auto:
                mc.setMCWind(mac, MCCommand.Wind.AUTO);
                break;
            case R.id.id_wind_low:
                mc.setMCWind(mac, MCCommand.Wind.LOW);
                break;
            case R.id.id_wind_high:
                mc.setMCWind(mac, MCCommand.Wind.HIGH);
                break;
            case R.id.id_wind_mid:
                mc.setMCWind(mac, MCCommand.Wind.MID);
                break;
            case R.id.id_wind_supper_high:
                mc.setMCWind(mac, MCCommand.Wind.SUPER_HIGH);
                break;
            case R.id.id_wind_mute:
                mc.setMCWind(mac, MCCommand.Wind.MUTE);
        }
    }

    @Click({R.id.id_mode_air,
            R.id.id_mode_auto,
            R.id.id_mode_dehumi,
            R.id.id_mode_humi,
            R.id.id_mode_purify_humi,
            R.id.id_mode_purify,
            R.id.id_mode_purify_dehumi
    })
    void setMode(View clickedView) {
        switch (clickedView.getId()) {
            case R.id.id_mode_auto:
                mc.setMCMode(mac, MCCommand.Mode.AUTO);
                break;
            case R.id.id_mode_air:
                mc.setMCMode(mac, MCCommand.Mode.AIR);
                break;
            case R.id.id_mode_dehumi:
                mc.setMCMode(mac, MCCommand.Mode.DEHUMI);
                break;
            case R.id.id_mode_humi:
                mc.setMCMode(mac, MCCommand.Mode.HUMI);
                break;
            case R.id.id_mode_purify:
                mc.setMCMode(mac, MCCommand.Mode.PURIFY);
                break;
            case R.id.id_mode_purify_dehumi:
                mc.setMCMode(mac, MCCommand.Mode.PURIFY_DEHUMI);
                break;
            case R.id.id_mode_purify_humi:
                mc.setMCMode(mac, MCCommand.Mode.PURIFY_HUMI);
                break;
        }
    }


    @Override
    public void refreshControlView(HashMap<String, uSDKDeviceAttribute> attributes) {
        if (attributes.get(MCCommand.CmdName.ENVIRONMENT_OPERATIVE_TEMPERATURE_ROOM) != null) {
            //设置温度
            String temperature = attributes.get(MCCommand.CmdName.ENVIRONMENT_OPERATIVE_TEMPERATURE_ROOM).getAttrvalue();
            tv_temperature.setText(temperature);
        }

        if (attributes.get(MCCommand.CmdName.SETTING_MODE) != null) {
            //模式
            String mode = attributes.get(MCCommand.CmdName.SETTING_MODE).getAttrvalue();
            if (MCCommand.CmdValue.SETTING_MODE_ZHINENG.equals(mode)) {
                setModeButtonBgColor(button_auto);
            } else if (MCCommand.CmdValue.SETTING_MODE_SONGFENG.equals(mode)) {
                setModeButtonBgColor(button_air);
            } else if (MCCommand.CmdValue.SETTING_MODE_CHUSHI.equals(mode)) {
                setModeButtonBgColor(button_dehumi);
            } else if (MCCommand.CmdValue.SETTING_MODE_JIASHI.equals(mode)) {
                setModeButtonBgColor(button_humi);
            } else if (MCCommand.CmdValue.SETTING_MODE_JINGHUA.equals(mode)) {
                setModeButtonBgColor(button_purify);
            } else if (MCCommand.CmdValue.SETTING_MODE_JINGHUA_CHUSHI.equals(mode)) {
                setModeButtonBgColor(button_purify_dehumi);
            } else if (MCCommand.CmdValue.SETTING_MODE_JINGHUA_JIASHI.equals(mode)) {
                setModeButtonBgColor(button_purify_humi);
            }

        }

        if (attributes.get(MCCommand.CmdName.SETTING_WIND) != null) {
            //模式
            String mode = attributes.get(MCCommand.CmdName.SETTING_WIND).getAttrvalue();
            if (MCCommand.CmdValue.SETTING_WIND_DIFENG.equals(mode)) {
                setWindButtonBgColor(button_wind_low);
            } else if (MCCommand.CmdValue.SETTING_WIND_GAOFENG.equals(mode)) {
                setWindButtonBgColor(button_wind_high);
            } else if (MCCommand.CmdValue.SETTING_WIND_JINGYIN.equals(mode)) {
                setWindButtonBgColor(button_wind_mute);
            } else if (MCCommand.CmdValue.SETTING_WIND_ZHONGFENG.equals(mode)) {
                setWindButtonBgColor(button_wind_mid);
            } else if (MCCommand.CmdValue.SETTING_WIND_QIANGJING.equals(mode)) {
                setWindButtonBgColor(button_wind_super_high);
            } else if (MCCommand.CmdValue.SETTING_WIND_ZIDONG.equals(mode)) {
                setWindButtonBgColor(button_wind_auto);
            }

        }

    }

    private void setModeButtonBgColor(TextView view) {
        button_auto.setTextColor(whiteColor);
        button_air.setTextColor(whiteColor);
        button_dehumi.setTextColor(whiteColor);
        button_humi.setTextColor(whiteColor);
        button_purify.setTextColor(whiteColor);
        button_purify_dehumi.setTextColor(whiteColor);
        button_purify_humi.setTextColor(whiteColor);
        view.setTextColor(redColor);
    }

    private void setWindButtonBgColor(TextView view) {
        button_wind_high.setTextColor(whiteColor);
        button_wind_low.setTextColor(whiteColor);
        button_wind_mid.setTextColor(whiteColor);
        button_wind_auto.setTextColor(whiteColor);
        button_wind_super_high.setTextColor(whiteColor);
        button_wind_mute.setTextColor(whiteColor);
        view.setTextColor(redColor);
    }

    @Override
    public String getBindMac() {
        return mac;
    }
}
